﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Core.DAL;
using Core;
using xManagerWebApp.ExpenseWorkflow;

namespace xManagerWebApp
{
    public partial class ExpenseReportControl : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                foreach (GridViewRow row in this.grid.Rows)
                {
                    Button b = (Button)row.FindControl("btnJustification");
                    Guid itemId = new Guid(b.CommandArgument);

                    String just = "";
                    using (DataAccessLayer dc = new DataAccessLayer())
                    {
                        just = ExpenseReport.RetrieveJustification(dc, itemId);
                    }

                    b.OnClientClick = "alert('" + just + "')";
                }
            }
            //btnDelete.Attributes.Add("onclick", "javascript:if(confirm('Are you sure?')== false) return false;");
        }

        protected void OnLinqDataSourceSelecting(object sender, LinqDataSourceSelectEventArgs e)
        {
            using (DataAccessLayer dal = new DataAccessLayer())
            {
                Guid userId = Core.User.CurrentUserId.Value;
                var query = Core.ExpenseReport.LoadSearchResult(dal, this.editSearchName.Text,
                    this.editSearchState.SelectedValue,userId);
                e.Result = query;
            }
        }

        private void SetEditMode(bool mode)
        {
            this.editPanel.Visible = mode;
            this.listPanel.Visible = !mode;
        }

        public override void DataBind()
        {
            using (DataAccessLayer dal = new DataAccessLayer())
            {
                this.editSearchState.DataSource = Core.ExpenseReport.GetAllStates(dal);
                this.editSearchState.DataBind();
                this.editSearchState.Items.Insert(0, string.Empty);
            }
        }



        #region EventHandlers
        protected void OnSearch(object sender, EventArgs e)
        {
            this.grid.DataBind();
        }

        protected void OnNew(object sender, EventArgs e)
        {
            this.controlExpenseReport.ExpRepId = null;
            this.controlExpenseReport.ClearFields();
            this.SetEditMode(true);
        }

        protected void OnSelected(object sender, EventArgs e)
        {
            Guid id = (Guid)grid.DataKeys[grid.SelectedIndex].Value;
            this.ViewState["PK"] = id;

            //TODO: Set the edit control Id here...
            this.controlExpenseReport.ExpRepId = id;
            this.controlExpenseReport.DataBind();

            this.SetEditMode(true);

        }

        protected void OnSave(object sender, EventArgs e)
        {
            //TODO: Call Edit control Save()
            this.controlExpenseReport.Save();
            
            this.SetEditMode(false);
            this.grid.DataBind();
        }

        protected void OnReturn(object sender, EventArgs e)
        {
            this.SetEditMode(false);
        }

        protected void OnSubmit(object sender, EventArgs e)
        {
            Button button = (Button)sender;
            Guid itemId = new Guid(button.CommandArgument);
            using (DataAccessLayer dal = new DataAccessLayer())
            {
                Core.ExpenseReport.Submit(dal, itemId);
                this.grid.DataBind();
                //this.DataBind();
            }

            ExpenseWorkflowContractClient client = new ExpenseWorkflowContractClient();
            client.SubmitExpense(itemId);
            client.Close();
        }

        protected void OnDelete(object sender, EventArgs e)
        {
            ImageButton imageButton = (ImageButton)sender;
            Guid itemId = new Guid(imageButton.CommandArgument);
            using (DataAccessLayer dal = new DataAccessLayer())
            {
                Core.ExpenseReport.Delete(dal, itemId);
                grid.DataBind();
            }
        }

        #endregion

    }
}